package uk.org.landeg.euler.problems.unported;
/**
 * Euler problem 46 -
 * Conjecture : all composite odd numbers can be written as the sum of a prime and twice a square
 *  (P(n) + 2x^2).
 * This is incorrect - find the first number that violates this conjecture.
 * @author Andy
 *
 */
public class Euler046 {
	public static void main(String[] args) {
		long l = 11; long lastl = 11;
		int maxSq;
		int sq;
		boolean finished  = false;
		do {
			if (!Utils.isPrime(l)) {
				finished = true;
				// composite number
				maxSq = (int)Math.sqrt(l);
				sq = maxSq + 1;
				theLoop : while (sq > 0) {
					if (Utils.isPrime(l - 2 * sq * sq)) {
						finished = false;
						break theLoop;
					}
					sq--;
				}
			} 
			if (lastl -l > 1000) {
				System.out.println(l);
				lastl = l;
			}
			if (!finished) l+=2;
			
		} while (!finished);
		System.out.println(l);
	}
}
